// Copyright (c) 2008 screamingtarget@gmail.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.


#pragma once

#include "Client.h"
#include <boost/thread.hpp>
#include <boost/function.hpp>
#include <memory>


const UINT RWM_SINGLEINSTANCE = RegisterWindowMessage( _T("RWM_SINGLEINSTANCE__A1B92E14_23B6_4a26_A088_91A36F817F4F") );
const UINT RWM_CLIENTPROCEXIT = RegisterWindowMessage( _T("RWM_CLIENTPROCEXIT__AF320481_339A_4271_9A11_20C4B5011B1B") );


class CMainDlg : public CDialogImpl< CMainDlg >, public CUpdateUI< CMainDlg >,
		public CMessageFilter, public CIdleHandler, CWinDataExchange< CMainDlg >
{
public:

	enum { IDD = IDD_MAINDLG };

	virtual BOOL PreTranslateMessage( MSG* pMsg );
	virtual BOOL OnIdle();

	BEGIN_UPDATE_UI_MAP( CMainDlg )
	END_UPDATE_UI_MAP()

	BEGIN_MSG_MAP( CMainDlg )
		MESSAGE_HANDLER( WM_INITDIALOG, OnInitDialog )
		MESSAGE_HANDLER( WM_DESTROY, OnDestroy )
		COMMAND_ID_HANDLER( IDCANCEL, OnCancel )
      MESSAGE_HANDLER( RWM_SINGLEINSTANCE, OnSingleInstance )
      COMMAND_HANDLER( IDC_IPADDRESS, EN_CHANGE, OnIpAddressChange )
      COMMAND_HANDLER( IDC_PORT, EN_UPDATE, OnPortUpdate )
      COMMAND_HANDLER( IDC_PORT, EN_KILLFOCUS, OnPortKillFocus )
      COMMAND_HANDLER( IDC_START, BN_CLICKED, OnStartClicked )
      MESSAGE_HANDLER( RWM_CLIENTPROCEXIT, OnClientProcExit )
      MESSAGE_HANDLER( RWM_CLIENTCONNECTED, OnClientConnected )
      MESSAGE_HANDLER( RWM_CLIENTSTARTED, OnClientStarted )
      COMMAND_HANDLER( IDC_STOP, BN_CLICKED, OnStopClicked )
      MESSAGE_HANDLER( RWM_AUDIODROPOUT, OnAudioDropout )
      MESSAGE_HANDLER( RWM_PORTAUDIOSTATUS, OnPortAudioStatus )
	END_MSG_MAP()

   BEGIN_DDX_MAP( CMainDlg )
      DDX_CONTROL_HANDLE( IDC_IPADDRESS, c_ipAddress )
      DDX_CONTROL_HANDLE( IDC_PORT, c_port )
      DDX_CONTROL_HANDLE( IDC_START, c_start )
      DDX_CONTROL_HANDLE( IDC_STOP, c_stop )
      DDX_CONTROL_HANDLE( IDC_CONNECTED, c_connected )
      DDX_CONTROL_HANDLE( IDC_DROPOUTS, c_dropouts )
      DDX_CONTROL_HANDLE( IDC_STATUS, c_status )
   END_DDX_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ )
//	LRESULT CommandHandler( WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/ )
//	LRESULT NotifyHandler( int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/ )

private:

   CIPAddressCtrl c_ipAddress;
   CEdit c_port;
   CButton c_start;
   CButton c_stop;
   CStatic c_connected;
   CStatic c_dropouts;
   CListBox c_status;

   UINT dropouts_;

   boost::function< void() > StopClient_;
   bool stopPressed_;

   enum
   {
      MinPort = 1,
      MaxPort = 65535
   };

	LRESULT OnInitDialog( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );
	LRESULT OnDestroy( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );
	LRESULT OnCancel( WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/ );

	void CloseDialog( int nVal );

   LRESULT OnSingleInstance( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );

   void PersistWindowPlacement();
   void PlaceWindow();

   void PersistSettings();
   void RetrieveSettings();

   void EnableStartButton();

   LRESULT OnIpAddressChange( WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/ );
   LRESULT OnPortUpdate( WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/ );
   LRESULT OnPortKillFocus( WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/ );

   void ClientProc( std::string ipAddress, std::string port );

   LRESULT OnStartClicked( WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/ );
   LRESULT OnStopClicked( WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/ );

   void EnableControls( BOOL enable = TRUE );

   LRESULT OnClientProcExit( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );

   void Connect();

   LRESULT OnClientConnected( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );

   void SetConnectedText( bool connected = true );

   bool AllFieldsFilled();
   bool PortFilled();

   LRESULT OnClientStarted( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );

   LRESULT OnAudioDropout( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );
   void SetDropoutText();

   LRESULT OnPortAudioStatus( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );
};
